<template>
    <van-nav-bar title="订单结算" left-text="返回" left-arrow @click-left="onClickLeft" />

    <van-contact-card type="edit" :tel="address.mobile" :name="address.consignee" @click="onOrder" v-if="JSON.stringify(address) != '{}'" />

    <van-contact-card type="add" @click="onOrder" v-else />

    <van-card
        v-for="item in CartList"
        :key="item.id"
        :num="item.pronum"
        :price="item.total"
        :title="item.product.name"
        :thumb="item.product.cover_cdn"
    >
        <template #desc>
            <span>商品单价：￥{{item.price}}</span>
        </template>
    </van-card>

    <van-cell-group inset>
    <van-field
        v-model="content"
        rows="2"
        autosize
        label="订单备注"
        type="textarea"
        maxlength="50"
        placeholder="请输入订单备注"
        show-word-limit
    />
    </van-cell-group>

    <van-submit-bar :price="total" button-text="结算" @submit="onSubmit" />

</template>
<script>
import {showNotify} from 'vant'
export default {
    created() {
        let address = this.$cookies.get('address') ? this.$cookies.get('address') : {}

        this.ids = this.$route.query.ids ? this.$route.query.ids : ''

        this.getConfirmData()

        if(JSON.stringify(address) == '{}')
        {
            this.getAddrData()
        }else{
            this.address = address
        }
    },
    data() {
        return {
            address:{},
            LoginUser:this.$cookies.get('LoginAuth'),
            ids:'',
            CartList:[],
            content:''
        }
    },
    methods: {
        onClickLeft()
        {
            this.$router.go(-1)
        },
        onOrder()
        {
            this.$router.push({path:'/user/address/index',query:{action:'order'}})
        },
        async getAddrData()
        {
            let result = await this.$api.AddressOrder({busid:this.LoginUser.id})

            if(result.code === 1)
            {
                this.address = result.data
            }
        },
        async getConfirmData()
        {
            let result = await this.$api.ConfirmOrder({ids:this.ids})

            if(result.code === 0)
            {
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }

            this.CartList = result.data
        },
        async onSubmit()
        {
            // 订单数据
            let data = {
                busid:this.LoginUser.id,
                cartid:this.ids,
                addrid:this.address.id,
                content:this.content
            }

            let result = await this.$api.OrderAdd(data)

            if(result.code === 1)
            {
                showNotify({
                    type:'success',
                    message:result.msg,
                    onClose:() => {
                        this.$router.push('/user/order/index')
                    }
                })

                return false
            }else{
                showNotify({
                    type:'warning',
                    message:result.msg
                })

                return false
            }
        }
    },
    computed:{
        total()
        {
            let total = 0

            if(this.CartList.length > 0)
            {
                for(let item of this.CartList)
                {
                    total += parseFloat(item.total)
                }
            }

            return total * 100
        }
    }
}
</script>